package cn.stronglink.asset.module.assetmanage.repair.service;

import java.util.Date;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.toolkit.IdWorker;

import cn.stronglink.asset.model.ItAsset;
import cn.stronglink.asset.model.Repair;
import cn.stronglink.asset.model.RepairDetail;
import cn.stronglink.asset.module.assetmanage.itasset.mapper.ItassetMapper;
import cn.stronglink.asset.module.assetmanage.itasset.vo.ItAssetVo;
import cn.stronglink.asset.module.assetmanage.repair.mapper.RepairMapper;
import cn.stronglink.suite.core.util.WebUtil;

/**
 * @author sunlei:
 * @version 创建时间：2017年11月13日 下午15:25:01 类说明 设备维修service
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class RepairService {

	@Autowired
	private RepairMapper repairMapper;

	@Transactional(propagation=Propagation.NOT_SUPPORTED,readOnly=true)
	public Page<Repair> queryList(Page<Repair> page, Map<String, Object> params) {
		List<Repair> list = repairMapper.queryList(page, params);
		page.setRecords(list);
		return page;
	}

	/***
	 * 新增维修主表
	 * 
	 * @param repair
	 * @return
	 */
	public int create(Repair repair) {
		repair.setId(IdWorker.getId());
		repair.setStatus(1);
		repair.setCreateBy(WebUtil.getCurrentUser());
		repair.setCreateTime(new Date());
		int count = repairMapper.insertRepair(repair);
		List<RepairDetail> detailList = repair.getDetailList();
		if (detailList != null && detailList.size() > 0) {
			for (RepairDetail detail : detailList) {
				detail.setId(IdWorker.getId());
				detail.setRepairId(repair.getId());
				detail.setAssetId(Long.parseLong(detail.getAssetId_()));
				detail.setStatus(2);
				detail.setCreateBy(WebUtil.getCurrentUser());
				detail.setCreateTime(new Date());
				repairMapper.insertRepairDetail(detail);
			}
		}
		return count;
	}

	/***
	 * 新增维修明细
	 * 
	 * @param detail
	 */
	@SuppressWarnings("unused")
	private void insertRepairDetail(RepairDetail repairDetail) {
		repairDetail.setCreateBy(WebUtil.getCurrentUser());
		repairDetail.setCreateTime(new Date());
		repairDetail.setStatus(2);
		repairMapper.insertRepairDetail(repairDetail);

	}

	/***
	 * 更新
	 * 
	 * @param repair
	 */
	public void edit(Repair repair) {
		repair.setUpdateTime(new Date());
		repair.setUpdateBy(WebUtil.getCurrentUser());
		// 更新维修单
		repairMapper.updateRepair(repair);
		// 删除维修详情
		repairMapper.removeRepairDetail(repair.getId());
	}

	/***
	 * 删除
	 * 
	 * @param id
	 */
	public void remove(Long id) {
		repairMapper.remove(id);
		repairMapper.removeRepairDetail(id);
	}

	/***
	 * 获取维修单主表信息
	 * @param id
	 */
	@Transactional(propagation=Propagation.NOT_SUPPORTED,readOnly=true)
	public Repair qryById(Long id) {
		Repair repair = repairMapper.qryById(id);
		repair.setDetailList(repairMapper.getDetailByRepairId(id));
		return repair;
	}

	/**
	 * 查询设备维修详情列表
	 */
	@Transactional(propagation=Propagation.NOT_SUPPORTED,readOnly=true)
	public List<RepairDetail> queryRepairDetialList(Long id) {
		List<RepairDetail> detailsList = repairMapper.getDetailByRepairId(id);
		return detailsList;
	}

	public void doRepair(Repair repair) {
		repair.setStatus(3);
		repair.setUpdateBy(WebUtil.getCurrentUser());
		repair.setUpdateTime(new Date());
		repairMapper.updateRepair(repair);
		if (repair.getDetailList() != null && repair.getDetailList().size() > 0) {
			for (RepairDetail detail : repair.getDetailList()) {
				detail.setStatus(1);
				detail.setUpdateBy(WebUtil.getCurrentUser());
				detail.setUpdateTime(new Date());
				repairMapper.updateRepairDetail(detail);
			}
		}
	}
	
	@Transactional(propagation=Propagation.NOT_SUPPORTED,readOnly=true)
	public Page<ItAssetVo> querAssetListByParam(Page<ItAssetVo> page,Map<String, Object> params) {
		page.setRecords(repairMapper.querAssetListByParam(page,params));
		return page;
	}

}
